﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.Utils;
using System.Text.RegularExpressions;
using Tedds_Tool.Automatables;
using LEET.LEETCommon.Model;
using LEET.LEETCommon;

using Compiled_Tests;
using LEET.PlayBack;
using Tedds_Tool.Instructions;
using LEET.LEETCommon.WidgetLookup;
using LEET.CommonInterfaces.TestControls;
using Tedds_Tool.Automatables.LEETModuals.LEETHackModuals;

namespace LEET.StandardModuals.Instructions
{
    public class Verify : Instruction
    {
        private string type;
        private string expected;
        private string elementType;
        private string element;
        private string property;
        private string assert;
        private IList<ILEETHackModual> hacks;

        public Verify(string[] args, IList<ILEETHackModual> hacks)
            : base(args)
        {
            this.hacks = hacks;
            assert = "Is";
            type = args[1].Trim();
            elementType = args[3].Trim();
            element = args[2].Trim().Replace("\"", "\\\"");
            property = args[4].Trim();
            if (args.Length == 6)
                expected = args[5].Trim();
            else
            {
                assert = args[5].Trim();
                expected = args[6].Trim();
                if (args.Length > 7)
                {
                    expected = "{" + args[6].Trim() + ", " + args[7].Trim() + "}";
                }
            }

            Match m = Regex.Match(expected, "\"(.*)\"");
            if (m.Success)
            {
                expected = LEET.LEETCommon.Utilities.Escape(m.Groups[1].Value);
            }
            else
            {
                expected = LEET.LEETCommon.Utilities.Escape(expected);
            }
            InstructionType = NodeTypes.VERIFY_TYPE;
        }

        public static IAutomationActionResult OrientationEquals(IBox box, String element, String expected)
        {
            AutomationElement elem = WidgetFinder.Find(element, box.RootElement);
            ValuePattern pattern = (elem.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern);
            pattern.SetValue("_____" + RotationHack.HACK_FLAG);
            Assert.AreEqual(expected, pattern.Current.Value);
            return new AutomationActionResult();
        }

        public static IAutomationActionResult SizeEquals(IBox box, String element, String expected)
        {
            AutomationElement elem = WidgetFinder.Find(element, box.RootElement);
            ValuePattern pattern = (elem.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern);
            pattern.SetValue("_____" + ScaleHack.HACK_FLAG);
            Assert.AreEqual(expected, pattern.Current.Value);
            return new AutomationActionResult();

        }

        public static IAutomationActionResult PositionEquals(IBox box, String element, String expected)
        {
            AutomationElement elem = WidgetFinder.Find(element, box.RootElement);
            ValuePattern pattern = (elem.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern);
            pattern.SetValue("_____" + PositionHack.HACK_FLAG);
            Assert.AreEqual(expected, pattern.Current.Value);
            return new AutomationActionResult();
        }

        public static IAutomationActionResult AreEqual(String expected, string actual)
        {
            Assert.AreEqual(expected, actual);
            return new AutomationActionResult();
        }

        public static IAutomationActionResult UnEqual(String expected, string actual)
        {
            Assert.AreNotEqual(expected, actual);
            return new AutomationActionResult();
        }

        public override String RenderStaticCall()
        {
            string actual;
            if ("".Equals(elementType))
            {
                actual = "WidgetFinder.Find(\"" + element + "\", Box.RootElement)" + property + ".ToString()";
            }
            else 
            {
                actual = "(WidgetFinder.Find(\"" + element + "\", Box.RootElement).GetCurrentPattern(" + elementType + ".Pattern) as " + elementType + ")" + property + ".ToString()";
            }
            
            string expected;
            if ("Is Not".Equals(assert))
                expected = this.expected;
            else if ("Is True".Equals(assert))
                expected = "True";
            else if ("Is False".Equals(assert))
                expected = "False";
            else// ("Is".Equals(assert))
                expected = this.expected;
            
            if ("MovePattern".Equals(elementType))
            {
                return "LEET.StandardModuals.Instructions.Verify.PositionEquals(Box, \""+element+"\", \""+expected+"\")";
            }
            else if ("ScalePattern".Equals(elementType))
            {
                return "LEET.StandardModuals.Instructions.Verify.SizeEquals(Box, \"" + element + "\", \"" + expected + "\")";
            }
            else if ("RotationPattern".Equals(elementType))
            {
                return "LEET.StandardModuals.Instructions.Verify.OrientationEquals(Box, \"" + element + "\", \"" + expected + "\")";
            }
            else
            {
                if ("Is Not".Equals(assert))
                    return "LEET.StandardModuals.Instructions.Verify.UnEqual(\"" + expected + "\", " + actual + ")";
                else
                    return "LEET.StandardModuals.Instructions.Verify.AreEqual(\"" + expected + "\", " + actual + ")";
            }
        }

        public override string PlainText()
        {
            string prop = property;
            if (property.Contains(".Current."))
                 prop = property.Remove(0, ".Current.".Length);
            string[] s = element.Split(',');//s[s.Length - 1]
            return "Checked to see if the \"" + prop + "\" property of \"" + element + "\" was \"" + expected + "\".";
        }

        public override bool Valid
        {
            get
            {
                Errors.Clear();
                if (!type.Equals("ELEMENT") || containsIllegalChar(type)){
                    AddErrorMsg("Invalid Type: " + type);
                }

                if (property.Contains(".Windows.Controls.ComboBoxItem:") || containsIllegalChar(property)){
                    AddErrorMsg("Invalid Property: " + property);
                }

                if (containsIllegalChar(element))
                {
                    AddErrorMsg("Invalid Element: " + element);
                }

                if (expected.Equals("--Arguments--"))
                    AddErrorMsg("Invalid Expectation: " + expected);

                return base.Valid;
            }
        }

        private bool containsIllegalChar(string str)
        {
            return str.Contains('+')
                //|| str.Contains('=')
                || str.Contains('\\')
                || str.Contains('?');
                //|| str.Contains('-');
        }
    }
}
